package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.OutStock;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Mr.Cao
 * @DateTime: 2021/11/10 14:40
 * BMW F750GS
 **/
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    private ConsumerMapper consumerMapper;

    /**
     * 分页带条件查询 出库消费记录
     */
    @Override
    public PageVO findInStockList(Map queryMap) {
        String pageNum = (String) queryMap.get("pageNum");
        String pageSize = (String) queryMap.get("pageSize");
        //分页
        Page<Object> page = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        //条件查询
        Example o = new Example(Consumer.class);
        Example.Criteria criteria = o.createCriteria();
        //具体地点
        String name = (String) queryMap.get("name");
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        //地址
        String address = (String) queryMap.get("address");
        if (!StringUtils.isEmpty(address)) {
            criteria.andLike("address", "%" + address + "%");
        }
        //联系人
        String contact = (String) queryMap.get("contact");
        if (!StringUtils.isEmpty(contact)) {
            criteria.andLike("contact", "%" + contact + "%");
        }
        List<Consumer> consumers = consumerMapper.selectByExample(o);
        //转Vo
        List<ConsumerVO> consumerVOList = consumers.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        return new PageVO(page.getTotal(), consumerVOList);
    }

    /**
     * 编辑时做数据的回显
     */
    @Override
    public ConsumerVO edit(long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        if (consumer != null) {
            //转Vo
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }
        return null;
    }

    /**
     * 编辑物资消费记录
     */
    @Override
    public void update(long id, ConsumerVO consumerVO) {
        if (consumerVO != null){
            Consumer consumer = new Consumer();
            BeanUtils.copyProperties(consumerVO,consumer);
            consumer.setModifiedTime(new Date());
            consumerMapper.updateByPrimaryKey(consumer);
        }
    }
    /**
     * 删除物资发放记录
     * */
    @Override
    public void delete(long id) {
        consumerMapper.deleteByPrimaryKey(id);
    }
    /**
     * 添加物资发放记录
     * */
    @Override
    public void add(ConsumerVO consumerVO) {
        if (consumerVO!=null){
            //转换
            Consumer consumer = new Consumer();
            BeanUtils.copyProperties(consumerVO,consumer);
            consumer.setCreateTime(new Date());
            consumer.setModifiedTime(new Date());
            consumerMapper.insert(consumer);
        }
    }

    @Override
    public List findAll() {
        List<Consumer> consumers = consumerMapper.selectAll();
        return consumers;
    }
}
